<?php
/**
 * @title 1448. 统计二叉树中好节点的数目
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function goodNodes($root) {
        /**
         * @since 2024-01-04 好节点的个数
         */
        $goodNodesCount = 0;

        /**
         * @since 2024-01-04 广度遍历
         * @since 2024-01-04 [TreeNode, maxValue]
         */
        $nodeArray = [[$root, $root->val]];
        while($nodeArray){
            /**
             * @since 2024-01-04 下次遍历的节点
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-04 遍历当前层的点
             */
            foreach ($nodeArray as list($node, $maxValue)){
                /**
                 * @since 2024-01-04 当前节点的值大于等于当前层的最大值
                 */
                if($node->val >= $maxValue){
                    /**
                     * @since 2024-01-04 好节点的个数+1
                     */
                    $goodNodesCount++;
                } else {}

                /**
                 * @since 2024-01-04 更新这条路径的最大值
                 */
                $maxValue = max($maxValue, $node->val);

                /**
                 * @since 2024-01-04 当前节点的左节点不为空
                 * @since 2024-01-04 将左节点加入下次遍历的节点
                 */
                if($node->left !== null){
                    $nextNodeArray[] = [$node->left, $maxValue];
                } else {}

                /**
                 * @since 2024-01-04 当前节点的右节点不为空
                 * @since 2024-01-04 将右节点加入下次遍历的节点
                 */
                if($node->right !== null){
                    $nextNodeArray[] = [$node->right, $maxValue];
                } else {}
            }

            /**
             * @since 2024-01-04 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return Integer
         */
        return $goodNodesCount;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.04_13.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
